﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.BO.Editoral.Category;

using Portal.User.Security;
using Portal.GUI.Users.Common;
using Portal.Core.DAL;

namespace Portal.GUI.EditoralOffice.MainOffce.Category
{
    public partial class categoryedit : System.Web.UI.UserControl
    {
        protected string ParentName = string.Empty;
        string userID = "", assignUser = "";
        private int cat_id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["NewsRef"] != null && Request.QueryString["NewsRef"] != string.Empty)
            {
                cat_id = Convert.ToInt32(Request.QueryString["NewsRef"].ToString());
            }

            userID = Session["id"] != null ? Session["id"].ToString() : "";
            //người gán

            
            ViewState["userid"] = assignUser = Page.User.Identity.Name;

            if (!IsPostBack)
            {
                LoadChannel(assignUser);
                ViewState["ParentId"] = 0;
                CategoryHelper.TreebuildAllCat(cboLayout);
                CategoryHelper.TreebuildAllCat_by_ChannelID(ddlParentID, ddlChannel.SelectedValue.ToString());
                cboLayout.Items[0].Text = string.Empty;
                BindData();
            }
        }

        private void BindData()
        {
            if (cat_id != 0)
            {
                ltrTitleEdit.Text = "Cập nhật chuyên mục";
                ArrayList dtCat = new ArrayList();
                dtCat = CategoryHelper.getCatInfo(cat_id);
                if (dtCat != null && dtCat.Count > 0)
                {
                    txtCatName.Text = dtCat[0].ToString();
                    txtCatDisplayURL.Text = dtCat[2].ToString();
                    txtCatOrder.Text = dtCat[9].ToString();
                    if (dtCat[6]!=null)
                        txtIcon.Text = dtCat[6].ToString();
                    ddlParentID.SelectedValue = dtCat[3].ToString();
                    btnAddNew.Text = "Cập nhật";
                    chkDisplay.Checked = Convert.ToBoolean(dtCat[5]);
                }
            }
            else
            {
                ltrTitleEdit.Text = "Thêm mới chuyên mục";
                btnAddNew.Text = "Thêm mới";
            }
        }



        #region Load Channel
        private void LoadChannel(string userID)
        {
            MainSecurity ms = new MainSecurity();
            if (userID != Const.Global_Admin && userID != Const.TONG_BIEN_TAP)
            {
                ddlChannel.DataSource = ms.GetChannelAsTable(userID);
                ddlChannel.DataBind();
                //BindCatListbyUserID(userID);


                ddlChannel.Enabled = false;

                ddlChannel.SelectedValue = ConfigurationManager.AppSettings["Channel"].ToString();
            }
            else
            {
                using (Xmobile.Users.MainDB db = new Xmobile.Users.MainDB())
                {
                    //ddlChannel.DataSource = db.ChannelCollection.GetAllAsDataTable();
                    ddlChannel.DataSource = db.StoreProcedure.vc_Execute_Sql("SELECT * FROM Channel");
                    ddlChannel.DataBind();
                    //BindListCatbyChannelID(ddlChannel.SelectedValue.Trim());
                    ddlChannel.SelectedValue = ConfigurationManager.AppSettings["Channel"].ToString();
                }
            }

        }
        #endregion

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            if (cat_id == 0)
            {
                CategoryHelper.CreateNewCat(
                txtCatName.Text,
                int.Parse(txtCatOrder.Text),
                string.Empty,
                txtCatDisplayURL.Text,
                Convert.ToInt32(ddlParentID.SelectedValue),
                int.Parse(cboEditionType.SelectedValue),
                txtIcon.Text.Trim(),
                false,
                Convert.ToBoolean(chkDisplay.Checked),
                cboLayout.SelectedIndex == 0 ? string.Empty : CategoryHelper.GetTabReferenceByCatId(int.Parse(cboLayout.SelectedValue)), Convert.ToInt32(ddlChannel.SelectedValue));
                Response.Redirect("/office/category-list.chn");
            }
            else
            {
                CategoryHelper.UpdateCategory(cat_id, txtCatName.Text.Trim(), txtCatDisplayURL.Text.Trim(), txtIcon.Text.Trim(), txtCatOrder.Text.Trim(), Convert.ToBoolean(chkDisplay.Checked));
                Response.Redirect("/office/category-list.chn");
            }

            
        }

       

        protected void ddlChannel_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void txtCatName_TextChanged(object sender, EventArgs e)
        {
            txtCatDisplayURL.Text = BO.UnicodeUtility.UnicodeToKoDauAndGach(txtCatName.Text).ToLower();
        }

        protected void btDel_Click(object sender, EventArgs e)
        {
            CategoryHelper.Delete(cat_id);
            Response.Redirect("/office/category-list.chn");
        }

        protected void btBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/office/category-list.chn");
        }
    }
}